package com.hollycrm.ahunicom.sheet.business.hibernate;

import java.util.List;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.ahunicom.sheet.business.SelectSheetFacadeLogic;
import com.hollycrm.ahunicom.sheet.domain.WSNameDept;
import com.hollycrm.ahunicom.sheet.facade.client.AbstractSheetFacadeProvider;
import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

public class HibernateSelectSheetFacadeLogic extends HibernateDaoSupport implements SelectSheetFacadeLogic  {

	/*
	 * (non-Javadoc)
	 * @see com.hollycrm.ahunicom.sheet.business.SelectSheetFacadeLogic#call(java.lang.String)
	 */
	public AbstractSheetFacadeProvider call(DoOperateContext doc) throws Exception {
		String deptID = (doc.getOutputAttr("deptID") == null) ? "":(String)doc.getOutputAttr("deptID");
		HibernateTemplate ht = getHibernateTemplate();
		String sheetFacadeName = null ;
		String beanID = null;
		AbstractSheetFacadeProvider sheetFacadeProvide = null;
		
		List wsNDList = ht.find("from o in " +WSNameDept.class +" where o.wsDeptId = '" +deptID.trim() + "'");
		if(wsNDList!= null && wsNDList.size()>0 && !wsNDList.isEmpty()){
        	WSNameDept wsND = (WSNameDept)wsNDList.get(0);
        	sheetFacadeName = wsND.getWsName();
        }else{
        	return null;
        }
		if(sheetFacadeName!=null){
			beanID = "sheetFacadeProvide" + sheetFacadeName.trim().toUpperCase() + "Logic";
			sheetFacadeProvide = (AbstractSheetFacadeProvider)DefaultBeanFactory.getBean(beanID);
	    } 
		 return sheetFacadeProvide;
	}

}
